import { apiExtend, request } from "./request";

/**
 * 登录账号信息
 */
export interface LoginAccountInfo {
  /** 用户id */
  id: number;
  /** 用户名 */
  name: string;
  /** 用户手机号 */
  mobile: string | number;
  /** 头像 */
  avatar: string;
  /** 用户登录凭证 */
  token: string;
  /** token刷新凭证 */
  refresh_token: string;
  /** 权限列表 */
  permission: (string | number)[];
  /** 角色id */
  role_id: number;
  /** 角色名 */
  role_name: string;
}

/** 账户登录参数 */
export interface AccountLoginParams {
  /** 验证码唯一标识 */
  verifyUuid: string;
  /** 账号 */
  account: string;
  /** 密码 */
  pwd: string;
  /** 验证码 */
  verify: string;
}

/** 登录 */
export const accountLogin = (data: AccountLoginParams) => {
  return request<LoginAccountInfo>({
    url: "/user/login",
    method: "POST",
    data,
  });
};

/** 刷新token */
export const refreshAccountToken = (headers: { Authorization: string }) => {
  return request<{
    token: string;
  }>({
    url: "/user/refresh",
    method: "POST",
    headers,
    uiConfig: {
      errorToast: false,
    },
  });
};

/** 注册参数 */
export interface RegisterParams {
  /** 账号 */
  account: string;
  /** 手机号 */
  mobile: string;
  /** 密码 */
  pwd: string;
  /** 验证码 */
  verify: string;
}

/** 注册 */
export const accountRegister = (data: RegisterParams) => {
  return request({
    url: "/user/register",
    method: "POST",
    data,
  });
};

/** 退出登录 */
export const accountLogout = () => {
  return request({
    url: "/user/logout",
    method: "POST",
  });
};

/** 获取登录账户信息 */
export const getLoginAccountInfo = () => {
  return request<{
    user_info: Partial<LoginAccountInfo>;
  }>({
    url: "/user/info",
    method: "GET",
  });
};

/** 发送验证码 */
export const sendVerifyCode = () => {
  return request({
    url: "/verify/getCode",
    method: "POST",
  });
};

/** 获取验证图 */
export const getVerifyImage = apiExtend(() => {
  return request<string>({
    url: "/verify/getImg",
    method: "POST",
  });
});

/** 修改密码参数 */
export interface ChangePwdParams {
  /** 老密码 */
  old_pwd: string;
  /** 新密码 */
  new_pwd: string;
}

/** 修改密码 */
export const changePwd = (data: ChangePwdParams) => {
  return request({
    url: "/user/changePwd",
    method: "POST",
    data,
  });
};
